//                       .::::.
//                     .::::::::.
//                    :::::::::::
//                 ..:::::::::::'
//              '::::::::::::'
//                .::::::::::
//           '::::::::::::::..
//                ..::::::::::::.
//              ``::::::::::::::::
//               ::::``:::::::::'        .:::.
//              ::::'   ':::::'       .::::::::.
//            .::::'      ::::     .:::::::'::::.
//           .:::'       :::::  .:::::::::' ':::::.
//          .::'        :::::.:::::::::'      ':::::.
//         .::'         ::::::::::::::'         ``::::.
//     ...:::           ::::::::::::'              ``::.
//    ```` ':.          ':::::::::'                  ::::..
//                       '.:::::'                    ':'````..
package com.xw.service;

import com.google.common.collect.Maps;
import com.xw.common.Const;
import com.xw.dao.TopupMapper;
import com.xw.dao.UserMapper;
import com.xw.model.Orders;
import com.xw.model.OrdersItem;
import com.xw.model.Topup;
import com.xw.model.User;
import com.xw.model.sdk.AliPayPCModel;
import com.xw.model.sdk.WeChatModel;
import com.xw.service.tool.alipay.AlipayService;
import com.xw.service.tool.wechat.WeChatService;
import com.xw.util.AppUtil;
import com.xw.util.JsonData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class TopupServiceImpl implements TopupService {

    @Autowired
    private TopupMapper topupMapper;
    @Autowired
    private AlipayService alipayService;
    @Autowired
    private WeChatService weChatService;
    @Resource
    private UserMapper userMapper;

    @Override
    public Map toTopup(HttpServletRequest request, String userId, Double money, Integer payType) throws Exception {

        //验证是否拉黑
        if (!StringUtils.equals("-1",userId) && StringUtils.isNotBlank(userId)) {
            User user = userMapper.selectByPrimaryKey(userId);
            if (user.getUserStatus() == 2) {
                return JsonData.returnMap(811, "您的账号已被锁定");
            }
        }
        if (money.doubleValue() < 0.01){
            return JsonData.returnMap(500,"充值金额不能小于0.01");
        }

        Map map = Maps.newHashMap();
        Topup topup = new Topup();
        topup.setId(AppUtil.uuid());
        topup.setUserId(userId);
        topup.setTopupType(0);
        topup.setMoney(money);
        String ordersId = AppUtil.getOrderCode();
        topup.setOrdersId(ordersId);
        topup.setCreateTime(new Date());

        if (payType==1){
            topupMapper.insertSelective(topup);
            //支付宝
            AliPayPCModel aliPayPCModel = new AliPayPCModel();
            aliPayPCModel.setBody("充值");
            aliPayPCModel.setSubject("充值");
            aliPayPCModel.setOutTradeNo(ordersId);
            //支付宝以元为单位
            aliPayPCModel.setTotalAmount(money.toString());
            aliPayPCModel.setNotifyUrl(Const.WEB_URL+"/aliPayCallBack/toTopup");
            map = alipayService.toPCPay(aliPayPCModel);
        }else if (payType==2){
            topupMapper.insertSelective(topup);
            //微信
            WeChatModel weChatModel = new WeChatModel();
            weChatModel.setBody("充值");
            //微信以分为单位
            weChatModel.setTotal_fee((int)(money * 100));
            weChatModel.setNotify_url(Const.WEB_URL+"/weChatCallBack/toTopup");
            weChatModel.setOut_trade_no(ordersId);
            map = weChatService.toNativePay(weChatModel,request);
        }
        return JsonData.success(map);
    }
}
